<?php

declare(strict_types=1);

namespace owns\middleware;

use owns\enum\SysEnum;
use owns\enum\RespCode;
use owns\interface\MiddlewareInterface;
use think\Exception;

/**
 * 客户端公共中间件
 */
class ApiMiddleware extends MiddlewareInterface
{
    /**
     * 处理请求信息
     * @param Request $request
     * @param string $token
     * @param Closure  $next
     * @return Response
     */
    public function handleRequest($request, $token, $next)
    {
        $request->port = SysEnum::PORT_MEMBER;
        $this->ipLimiting($request);
        $request->uid = 0; //未登录
        $request->auth = null; //无账户信息
        if ($token) {
            try {
                ApiAuthMiddleware::authority($request, $token);
            } catch (Exception) {
                tips('请重新登录', RespCode::NOT_LOGIN);
            }
        }
        return $next($request);
    }
}
